import java.util.HashMap;
import java.util.Map;

public class RemoveDuplicates {

    public int solution(int[] nums){
        if(nums==null||nums.length==0){
            return 0;
        }
        int left=0;
        for (int right = 1; right < nums.length; right++) {
            if(nums[left]!=nums[right]){
                nums[++left]=nums[right];
            }
        }
        return left+1;
    }

    public static int solution2(int[] nums){
        int n=nums.length;
        if(n<=2){
            return n;
        }
        int p1=2;
        int p2=2;
        while (p1<n){
            if(nums[p2-2]!=nums[p1]){
                nums[p2]=nums[p1];
                p2++;
            }
            p1++;
        }

        return p2;

    }

    public static int solution3(int[] nums){
        Map<Integer,Integer> map=new HashMap<>();
        int res=0;
        int p1=0;
        int p2=0;
        while (p1<nums.length&&p2<nums.length){
            Integer temp = map.getOrDefault(nums[p1],0);
            temp++;
            map.put(nums[p1],temp);
            if(temp<=2){
             p1++;
             p2++;
            }else if (nums[p2] != nums[p1]) {
              nums[p2] = nums[p1];
              p2++;
            }else {
                p1++;

            }
        }
        return p2;
    }

    public static void main(String[] args) {
        int[] nums=new int[]{0,0,1,1,1,1,2,3,3};
        System.out.println(solution3(nums));
    }
}
